package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.mybatis.callcenter.entity.WorkOrderInstallPic;
import cn.com.nes.mybatis.callcenter.mapper.WorkOrderInstallPicMapper;
import cn.com.nes.site.entity.em.InstallPictureEnum;
import cn.com.nes.site.entity.vo.workOrder.SaveWorkOrderInstallPicVO;
import cn.com.nes.site.entity.vo.workOrder.UpdatePicInfoVO;
import cn.com.nes.site.service.workOrder.CallWorkOrderPicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 长效管护安装工单服务
 */
@Service
@Slf4j
public class CallWorkOrderPicServiceImpl extends ServiceImpl<WorkOrderInstallPicMapper, WorkOrderInstallPic> implements CallWorkOrderPicService {

    /**
     * 保存安装工单图片信息 - 删除旧的，插入新的
     * @param vo
     */
    @Override
    public void insertPicInfo(UpdatePicInfoVO vo) throws Exception {
        // 删除旧的图片
        QueryWrapper<WorkOrderInstallPic> wrapper = new QueryWrapper<>();
        wrapper.eq("work_order_install_id", vo.getWorkOrderId());
        wrapper.eq("deleted", false);
        List<WorkOrderInstallPic> picList = this.baseMapper.selectList(wrapper);
        for (WorkOrderInstallPic item : picList) {
            item.setDeleted(Boolean.TRUE);
            Integer rows = this.baseMapper.updateById(item);
            if (!rows.equals(1)) {
                throw new Exception("保存安装工单图片异常!");
            }
        }
        // 保存新的图片
        for (SaveWorkOrderInstallPicVO item : vo.getSavePicList()) {
            WorkOrderInstallPic workOrderInstallPic = new WorkOrderInstallPic();
            workOrderInstallPic.setWorkOrderInstallId(vo.getWorkOrderId());
            workOrderInstallPic.setPath(item.getPath());
            workOrderInstallPic.setField(item.getField());
            workOrderInstallPic.setRemark(InstallPictureEnum.getVal(item.getField()));
            workOrderInstallPic.setDeleted(Boolean.FALSE);
            workOrderInstallPic.setGmtCreateId(vo.getUserInfo().getCallCenterUserId());
            workOrderInstallPic.setGmtCreate(vo.getOperateDate());
            workOrderInstallPic.setGmtModifiedId(vo.getUserInfo().getCallCenterUserId());
            workOrderInstallPic.setGmtModified(vo.getOperateDate());

            Integer rows = this.baseMapper.insert(workOrderInstallPic);
            if (!rows.equals(1)) {
                throw new Exception("保存安装工单图片异常!");
            }
        }
    }

}
